package com.example.client;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class SendMmsActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView img;
    private Button btn_send;
    private EditText et_phone;
    private Uri pic_uri;
    private EditText et_title;
    private EditText et_message;
    ActivityResultLauncher<Intent> mResultLanucher;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_send_mms);
         mResultLanucher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult o) {
                     if (o.getResultCode() == RESULT_OK){
                         Intent intent = o.getData();
                         pic_uri = intent.getData();
                         if (pic_uri != null){
                             img.setImageURI(pic_uri);
                             Log.e("TAG", "onActivityResult: "+pic_uri );
                         }
                     }
            }
        });
         et_message = findViewById(R.id.et_message);
         et_phone = findViewById(R.id.et_number);
         et_title = findViewById(R.id.et_title);

        img = findViewById(R.id.image);
        btn_send = findViewById(R.id.btn_send);
        img.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        // 条转到系统相册
        if(v.getId() == R.id.image){
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            //打开系统相册等待选择结果
            mResultLanucher.launch(intent);
        }else if (v.getId() == R.id.btn_send){
            sendMms(et_phone.getText().toString(),
                    et_title.getText().toString(),
                    et_message.getText().toString());
        }
    }

    private void sendMms(String phone, String title, String message) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 设置可读URI
        intent.putExtra("phone",phone);
        intent.putExtra("title",title);
        intent.putExtra("message",message);

        intent.putExtra(Intent.EXTRA_STREAM,pic_uri);
        intent.setType("image/*");
        startActivity(intent);

    }
}